<?php

namespace backend\models;

use Yii;
use yii\base\Model;
/**
 * This is the model class for table "base_sys_preview".
 *
 * @property integer $id
 * @property integer $item_id
 * @property integer $fid
 * @property integer $mid
 * @property integer $img_small_id
 * @property integer $img_thumb_id
 * @property integer $img_url_id
 */
class ItemPreview extends \yii\db\ActiveRecord
{
	public $file;
	
	public $small_img_id;
	public $thumb_img_id;
	public $big_img_id;
	public $b_t_s_img_id;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'base_sys_preview';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['item_id', 'img_small_id', 'img_thumb_id', 'img_url_id','small_img_id','thumb_img_id','big_img_id','b_t_s_img_id'], 'integer'],
            [['item_id'], 'required'],
            [['coating'], 'string', 'max' => 100],
            [['file'], 'required', 'on'=>'need_file',],
            [['file'], 'image','extensions' => 'png,jpg,jpeg,gif,bmp','maxSize'=>1024*1024*10,'tooBig'=>'超过10M',/*'maxWidth'=>'2000','maxHeight'=>'2000',
            'overWidth'=>'宽必须低于2000像素','overHeight'=>'高必须低于2000像素'*/],
            [['fid', 'mid', 'item_id'], 'unique', 'targetAttribute' => ['fid', 'mid', 'item_id'], 'message' => Yii::t('app','The combination of Item ID, Fid and Mid has already been taken.')]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'item_id' => Yii::t('app','Item ID'),
            'fid' => Yii::t('app','Fid'),
            'mid' => Yii::t('app','Mid'),
            'img_small_id' => 'Img Small ID',
            'img_thumb_id' => 'Img Thumb ID',
            'img_url_id' => 'Img Url ID',
            'file' => Yii::t('app','Image File'),
            'coating' => Yii::t('app','Coating'),
        ];
    }
    
    public function scenarios()
    {
    	$scenarios = parent::scenarios();
    	$scenarios['need_file'];
    	return $scenarios;
    }
    //与sys_image图相联系
    public function getUrlImage()
    {
    	return $this->hasOne(Image::className(),['id'=>'img_url_id']);
    }
    public function getThumbImage()
    {
    	return $this->hasOne(Image::className(),['id'=>'img_thumb_id']);
    }
    public function getSmallImage()
    {
    	return $this->hasOne(Image::className(),['id'=>'img_small_id']);
    }
    
    public function getFabric()
    {
    	return $this->hasOne(Fabric::className(),['id'=>'fid']);
    }
    public function getMaterial()
    {
    	return $this->hasOne(Material::className(),['id'=>'mid']);
    }
    public function getTzImage()
    {
    	return $this->hasOne(Image::className(),['id'=>'img_tz_id']);
    }
    //由fab_id和mat_id获取对应图片（数据表中）和面料图，材质图，返回json
   public static function  find_img_from_fab_mat($fab_id,$mat_id)
   	{
   		$_arr = array();
   		if(!empty($fab_id) && !empty($mat_id)){
   			$fab_img = Fabric::findOne(['id'=>$fab_id]);
   			$mat_img = Material::findOne(['id'=>$mat_id]);
    		$preview_img =  ItemPreview::findOne(['fid'=>$fab_id,'mid'=>$mat_id]);

   		}
   		if(empty($fab_id) && !empty($mat_id)){
   			$fab_id=0;
   			$fab_img ='';
   			$mat_img = Material::findOne(['id'=>$mat_id]);
   			$preview_img = ItemPreview::find()->where("mid = {$mat_id} and (fid={$fab_id} or fid is null)")->one();
   		}
   		if(!empty($fab_id) && empty($mat_id)){
   			$mat_id=0;
   			$fab_img = Fabric::findOne(['id'=>$fab_id]);
   			$mat_img ='';
   			$preview_img= ItemPreview::find()->where("fid = {$fab_id} and (mid={$mat_id} or mid is null)")->one();
   		}
   		$_arr['fab_img']=!empty($fab_img)?$fab_img:'';
   		$_arr['mat_img']=!empty($mat_img)?$mat_img:'';
   		$_arr['preview_img']=!empty($preview_img)?$preview_img:'';;
   		return $_arr;
    } 
}
